package com.gym.controller.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOCourse;
import com.gym.model.dao.DAOTrainer;
import com.gym.model.dao.DatoNonTrovatoException;
import com.gym.model.dao.MetodoDaoException;
import com.gym.model.entity.Course;
import com.gym.model.entity.Trainer;

public class dominiUpdateTrainerToCourse extends Action  {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		int id=Integer.parseInt(request.getParameter("id"));
		
		try {
			DAOCourse daoc = new DAOCourse();
			
			List<Course> lista=(List<Course>) daoc.selectToTrainer(id);
			
			for(Course c : lista)
			{
				c.setIdtrainer(-1);
				c.setTrainer(null);
				daoc.update(c);
				
			}
			DAOTrainer dao = new DAOTrainer();
			Trainer TrainerCancellato = dao.delete(id);
			request.getSession().setAttribute("TrainerCancellato", TrainerCancellato);
			request.setAttribute("course", lista);
			request.setAttribute("feedback", "ATTENZIONE ALCUNI CORSI SONO SENZA ISTRUTTORE");
			return mapping.findForward("success");
			
		} catch (ConnessioneException | MetodoDaoException | DatoNonTrovatoException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("failure");
		}
		
	}
}
